test('Setup OK', function() {
	ok(true, 'Setup OK');
});

test('initial 10x10 board', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	equal(view.initialBoard.getWidth(), 10, 'We expect default board to be 10x10');
	equal(view.initialBoard.getHeight(), 10, 'We expect default board to be 10x10');
});

test('First move', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);
	game.move(5, 5);

	equal(view.gameBoard.get(5, 5), Player.X, 'We expect X player to have occupied (5,5) square');
	equal(game.getCurrentPlayer(), Player.O, 'We expect O to be the next player');
});

test('Move on occupied square', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);
	game.move(5, 5);
	game.move(5, 5);

	equal(view.gameBoard.get(5, 5), Player.X, 'We expect X player to have occupied (5,5) square');
	equal(game.getCurrentPlayer(), Player.O, 'We expect O to be the next player');
});

test('Player commutation', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	equal(game.getCurrentPlayer(), Player.X, 'We expect X to be the first player');

	game.move(5, 5);
	equal(game.getCurrentPlayer(), Player.O, 'We expect O to be the next player');

	game.move(6, 5);
	equal(view.gameBoard.get(6, 5), Player.O, 'We expect O player to have occupied (6,5) square');
	equal(game.getCurrentPlayer(), Player.X, 'We expect X to be the current player');

});

test('Player X wins first col', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	game.move(0, 0);
	game.move(0, 6);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(1, 0);
	game.move(1, 6);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(2, 0);
	game.move(2, 6);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(3, 0);
	game.move(3, 6);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(4, 0);
	ok(view.winningPlayer == Player.X, 'We expect X to have just won.');

});

test('Player X wins on col', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	var start = 2;

	game.move(start, 5);
	game.move(start, 6);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(start + 1, 5);
	game.move(start + 1, 6);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(start + 2, 5);
	game.move(start + 2, 6);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(start + 3, 5);
	game.move(start + 3, 6);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(start + 4, 5);
	ok(view.winningPlayer == Player.X, 'We expect X to have just won.');

});

test('Player O wins on row', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	var start = 2;

	game.move(1, start);
	game.move(2, start);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(1, start + 1);
	game.move(2, start + 1);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(1, start + 2);
	game.move(2, start + 2);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(1, start + 3);
	game.move(2, start + 3);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(0, start + 4);
	game.move(2, start + 4);
	ok(view.winningPlayer == Player.O, 'We expect O to have just won.');

});

test('Player X wins on top rigth L-R diagonal', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	game.move(1, 2);
	game.move(2, 1);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(2, 3);
	game.move(3, 2);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(3, 4);
	game.move(4, 3);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(4, 5);
	game.move(5, 4);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(5, 6);
	ok(view.winningPlayer == Player.X, 'We expect X to have just won.');

	var winningCells = [];
	for (var i = 0; i < 5; i++) {
		winningCells.push({
			row : 1 + i,
			col : 2 + i
		});
	}
	deepEqual(view.winningCells, winningCells, 'We expect to have a (1,1) -> (5,5) winning diagonal');

});

test('Player X wins on bottom down L-R  diagonal', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	game.move(1, 0);
	game.move(2, 0);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(2, 1);
	game.move(3, 1);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(3, 2);
	game.move(4, 2);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(4, 3);
	game.move(5, 3);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(5, 4);
	ok(view.winningPlayer == Player.X, 'We expect X to have just won.');

});

test('Player X wins on diagonal top left R-L', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	game.move(1, 6);
	game.move(0, 9);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(2, 5);
	game.move(1, 9);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(3, 4);
	game.move(2, 9);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(4, 3);
	game.move(3, 9);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(5, 2);
	ok(view.winningPlayer == Player.X, 'We expect X to have just won.');

});

test('Player X wins on diagonal bottom right R-L', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(10, 10);

	game.move(2, 8);
	game.move(2, 1);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(3, 7);
	game.move(3, 2);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(4, 6);
	game.move(4, 3);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(5, 5);
	game.move(5, 4);
	ok(view.winningPlayer == null, 'No player has won yet');

	game.move(6, 4);
	ok(view.winningPlayer == Player.X, 'We expect X to have just won.');

});

test('Game ends on stale', function() {
	var view = new TestView();
	var game = new TicTacToe(view);

	game.start(3, 3);

	game.move(0, 0);
	game.move(1, 0);
	game.move(2, 0);
	game.move(0, 1);
	game.move(1, 1);
	game.move(2, 1);
	game.move(0, 2);
	game.move(1, 2);
	game.move(2, 2);

	ok(view.onStaleCalled, 'No player has won yet');

});

function TestView() {
	this.onStaleCalled = false;
}

TestView.prototype.init = function(game) {
	this.initialBoard = game.board;
};

TestView.prototype.update = function(gameBoard) {
	this.gameBoard = gameBoard;
};

TestView.prototype.onWin = function(winningPlayer, winningCells) {
	this.winningPlayer = winningPlayer;
	this.winningCells = winningCells;
};

TestView.prototype.onStale = function() {
	this.onStaleCalled = true;
};